﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Text;
using FMS.Formulation.Licensing.Engine;
using FMS.Formulation.Licensing.Engine.WCFLicenseService;
using FMS.Formulation.Licensing.Objects;

using System.DirectoryServices.ActiveDirectory;
using System.ServiceModel;
using System.Windows.Forms;
using Desaware.MachineLicense40;

namespace WindowsFormsApp1
{
    internal class Main
    {
        public void Run()
        {
            Console.WriteLine("Hello, World!");
            FMS.Formulation.Licensing.License.License license = new FMS.Formulation.Licensing.License.License();
            license.UsingLicenseServer = true;
            license.CompanyName = "FMS";
            license.ActivationCode = "123456";
            string aespswd = "fij2AO0d[123 4e823r0E*]uoMC!@98";

            ClientConfiguration theConfiguration = new ClientConfiguration();

            license.LicenseServer = theConfiguration.LicenseServer;
            license.LicenseServerPort = theConfiguration.TcpPort;

            RequestLicensePackage requestLicensePackage = new RequestLicensePackage();
            RequestLicense requestLicense = new RequestLicense();
            requestLicense.SessionID = Process.GetCurrentProcess().SessionId.ToString(CultureInfo.InvariantCulture);
            requestLicense.ClientDate = DateTime.UtcNow;
            requestLicense.Domain = GetComputerDomain();
            requestLicense.UserID = Environment.UserName;
            requestLicense.UserDomain = GetUserDomain();
            requestLicense.MachineName = Environment.MachineName;
            string actionObject = requestLicensePackage.Serialize(requestLicense, aespswd);
            ActionPackage actionPackage = new ActionPackage();
            actionPackage.theAction = ActionType.GetLicense;
            actionPackage.actionObject = actionObject;


            bool codeReUseWarning = false;
            ClientManager clientManager = new ClientManager();
            bool v = clientManager.ActivateLicense("abcd", ref codeReUseWarning);
            Console.Write(v);

            
            

            //WCFLicenseServiceClient wCFLicenseServiceClient = CreateWCFClient(theConfiguration);
            //actionPackage = wCFLicenseServiceClient.PerformAction(actionPackage);
            //Console.WriteLine("End!");

            //Desaware.MachineLicense40 ma = new Desaware.MachineLicense40();


            //configuration
            //clientManager.GetServerLicense(configuration);
            //
            // String body =
            //     "kyQf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VUZWjOHWSOpWLS1DyUTqQN1qZ6xYhQoXTMkYnl7mcz0+bTaSPR3kXV93ciGsMuV6OCAvqF+kHZI659XkrW7R8pNKBlMbl8Ap63fRRAcMjyIk7FBDkaifOTG6OEFl3z9UTBqd0Z5qcuRo5ye7aynsN8o/3h4WAd0TrXL7veRMXQA4FyRlRgJH8+LUoFcrrZx9pIvTItctJjpI/6cW/7C/WJh2MLslJYFAp3OQB/+L6M04y68m6biZuCkKfFixVcQ2DNG1K9Cm4lvnQmytSKJQ99RAXOQYBLuF0K0Zog0Q3K4/pZA118MemJImaV4MVioX3I4hbRzK086R3c04RIRxkaVg6CmpOAaSZVHXUlW8AKZmZkD/zwc854Gk9XXiBR410X9or6yOp9ZhiCSaI1NuywAvKrdWOYt5NXPmRxxZI1NmRptEY+XsFyBRTLO9hekskGKXCtl7XbzqRxyk3EXonkNPcgrMcI9TcXCIMvlelGx5x/e9Due6lHBJ7so1rww0eTmTUWxqS+oI=";
            // ReturnLicense rs = returnLicensePackage.DeSerialize(body, aespswd);
        }
        private string GetComputerDomain()
        {
            // try
            // {
            //     Domain computerDomain = Domain.GetComputerDomain();
            //     if (computerDomain != null)
            //     {
            //         return computerDomain.Name;
            //     }
            //     return string.Empty;
            // }
            // catch
            // {
            //     return string.Empty;
            // }
            return "abcd";
        }

        private string GetUserDomain()
        {
            try
            {
                return Environment.UserDomainName;
            }
            catch
            {
                return string.Empty;
            }
        }
        private WCFLicenseServiceClient CreateWCFClient(ClientConfiguration theConfiguration)
        {
            BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
            basicHttpBinding.Name = "basicHttpBinding_IWCFLicenseService_IWCFLicenseService";
            basicHttpBinding.CloseTimeout = new TimeSpan(0, 1, 0);
            basicHttpBinding.OpenTimeout = new TimeSpan(0, 1, 0);
            basicHttpBinding.ReceiveTimeout = new TimeSpan(0, 10, 0);
            basicHttpBinding.SendTimeout = new TimeSpan(0, 10, 0);
            basicHttpBinding.BypassProxyOnLocal = false;
            // basicHttpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            basicHttpBinding.MaxBufferSize = int.MaxValue;
            basicHttpBinding.MaxBufferPoolSize = 524288L;
            basicHttpBinding.MaxReceivedMessageSize = 2147483647L;
            basicHttpBinding.MessageEncoding = WSMessageEncoding.Text;
            basicHttpBinding.TextEncoding = Encoding.UTF8;
            basicHttpBinding.UseDefaultWebProxy = true;
            basicHttpBinding.TransferMode = TransferMode.Buffered;
            basicHttpBinding.Security.Mode = BasicHttpSecurityMode.None;
            string text = theConfiguration.LicenseServer;
            if (text.Length < 1)
            {
                text = "localhost";
            }
            UriBuilder uriBuilder = new UriBuilder("http", text, theConfiguration.TcpPort, "FS.Formulation.LicenseServer");
            return new WCFLicenseServiceClient(basicHttpBinding, new EndpointAddress(uriBuilder.Uri));
        }
    }
}
